package com.example.management.oa.controller

import com.example.management.common.config.Constant
import com.example.management.common.controller.BaseController
import com.example.management.common.service.DictService
import com.example.management.common.service.NotifyRecordService
import com.example.management.common.utils.PageUtils
import com.example.management.common.utils.Query
import com.example.management.common.utils.R
import com.example.management.oa.domain.NotifyDO
import com.example.management.oa.domain.NotifyRecordDO
import com.example.management.oa.service.NotifyService
import org.apache.shiro.authz.annotation.RequiresPermissions
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
import springfox.documentation.annotations.ApiIgnore
import java.util.*


@ApiIgnore
@Controller
@RequestMapping("/oa/notify")
class NotifyController : BaseController() {
    @Autowired
    private val notifyService: NotifyService? = null
    @Autowired
    private val notifyRecordService: NotifyRecordService? = null
    @Autowired
    private val dictService: DictService? = null

    @GetMapping
    @RequiresPermissions("oa:notify:notify")
    internal fun oaNotify(): String {
        return "oa/notify/notify"
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("oa:notify:notify")
    fun list(@RequestParam params: Map<String, Any>): PageUtils {
        // 查询列表数据
        val query = Query(params)
        val notifyList = notifyService!!.list(query)
        val total = notifyService!!.count(query)
        return PageUtils(notifyList, total)
    }

    @GetMapping("/add")
    @RequiresPermissions("oa:notify:add")
    internal fun add(): String {
        return "oa/notify/add"
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("oa:notify:edit")
    internal fun edit(@PathVariable("id") id: Long?, model: Model): String {
        val notify = notifyService!!.get(id)
        val dictDOS = dictService!!.listByType("oa_notify_type")
        val type = notify.type
        for (dictDO in dictDOS) {
            if (type == dictDO.getValue()) {
                dictDO.setRemarks("checked")
            }
        }
        model.addAttribute("oaNotifyTypes", dictDOS)
        model.addAttribute("notify", notify)
        return "oa/notify/edit"
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("oa:notify:add")
    fun save(notify: NotifyDO): R {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        notify.createBy = (getUserId())
        return if (notifyService!!.save(notify) > 0) {
            R.ok()
        } else R.error()
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("oa:notify:edit")
    fun update(notify: NotifyDO): R {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        notifyService!!.update(notify)
        return R.ok()
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("oa:notify:remove")
    fun remove(id: Long?): R {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        return if (notifyService!!.remove(id) > 0) {
            R.ok()
        } else R.error()
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("oa:notify:batchRemove")
    fun remove(@RequestParam("ids[]") ids: Array<Long>): R {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序")
        }
        notifyService!!.batchRemove(ids)
        return R.ok()
    }

    @ResponseBody
    @GetMapping("/message")
    internal fun message(): PageUtils {
        val params = HashMap<String, Any>(16)
        params["offset"] = 0
        params["limit"] = 3
        val query = Query(params)
        query.put("userId", getUserId()!!)
        query.put("isRead", Constant.OA_NOTIFY_READ_NO)
        return notifyService!!.selfList(query)
    }

    @GetMapping("/selfNotify")
    internal fun selefNotify(): String {
        return "oa/notify/selfNotify"
    }

    @ResponseBody
    @GetMapping("/selfList")
    internal fun selfList(@RequestParam params: Map<String, Any>): PageUtils {
        val query = Query(params)
        query.put("userId", this!!.getUserId()!!)

        return notifyService!!.selfList(query)
    }

    @GetMapping("/read/{id}")
    @RequiresPermissions("oa:notify:edit")
    internal fun read(@PathVariable("id") id: Long?, model: Model): String {
        val notify = notifyService!!.get(id)
        //更改阅读状态
        val notifyRecordDO = NotifyRecordDO()
        notifyRecordDO.notifyId=id
        notifyRecordDO.userId =(getUserId())
        notifyRecordDO.readDate = (Date())
        notifyRecordDO.isRead = (Constant.OA_NOTIFY_READ_YES)
        notifyRecordService!!.changeRead(notifyRecordDO)
        model.addAttribute("notify", notify)
        return "oa/notify/read"
    }


}